package com.xxl.sso.server.service.impl;

import com.alibaba.algo.cooperation.feign.vo.req.SelectUserInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.RpcUserInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.service.UserFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserLoginReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.LoginUserInfoResVO;
import com.alibaba.fastjson.JSON;
import com.xxl.sso.server.core.model.UserInfo;
import com.xxl.sso.server.core.result.ReturnT;
import com.xxl.sso.server.service.UserService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;


@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserFeignService userFeignService;

    @Override
    public ReturnT<UserInfo> findUser(String username, String password) {

        if (username == null || username.trim().isEmpty()) {
            return new ReturnT<>(ReturnT.FAIL_CODE, "登录账号不能为空");
        }
        if (password == null || password.trim().isEmpty()) {
            return new ReturnT<>(ReturnT.FAIL_CODE, "登录密码不能为空");
        }
        SelectUserLoginReqVO selectUserLoginReqVO = new SelectUserLoginReqVO();
        selectUserLoginReqVO.setUserAccount(username);
        selectUserLoginReqVO.setPassWd(password);
        return findUser(selectUserLoginReqVO);
    }

    private ReturnT<UserInfo> findUser(SelectUserLoginReqVO selectUserLoginReqVO) {
        log.info("登录请求--->{}", JSON.toJSONString(selectUserLoginReqVO));
        BaseResult<LoginUserInfoResVO> baseResult = userFeignService.checkLogin(selectUserLoginReqVO);
        log.info("登录返回--->{}", JSON.toJSONString(baseResult));
        if (baseResult.getCode().equals(0)) {
            LoginUserInfoResVO loginUserInfoResVO = baseResult.getData();
            if (loginUserInfoResVO == null || loginUserInfoResVO.getUserState() != 1) {
                return new ReturnT<>(ReturnT.FAIL_CODE, "账号尚未激活，请联系管理员。");
            }
            UserInfo userInfo = new UserInfo();
            userInfo.setUserid(Long.valueOf(String.valueOf(loginUserInfoResVO.getUserId())));
            userInfo.setUsername(String.valueOf(loginUserInfoResVO.getUserName()));
            userInfo.setUserCode(baseResult.getData().getUserCode());
            userInfo.setEmployeeId(loginUserInfoResVO.getEmployeeId());
            return new ReturnT<>(userInfo);
        }
        return new ReturnT<>(ReturnT.FAIL_CODE, "用户名或密码错误。");
    }


    /**
     * 钉钉登录
     */
    @Override
    public ReturnT<UserInfo> findUser(String code) {
        log.info("ding code:{}", code);
        SelectUserLoginReqVO selectUserLoginReqVO = new SelectUserLoginReqVO();
        selectUserLoginReqVO.setUserAccount(code);
        selectUserLoginReqVO.setType(2);
        return findUser(selectUserLoginReqVO);
    }

    @Override
    public ReturnT<UserInfo> findUserByEmployeeCode(String employeeCode) {
        log.info("UserService findUserByEmployeeCode employeeCode:{}", employeeCode);
        SelectUserInfoReqVO selectUserInfoReqVO = new SelectUserInfoReqVO();
        selectUserInfoReqVO.setEmployeeCode(employeeCode);
        BaseResult<RpcUserInfoVO> result = userFeignService.qryUserInfo(selectUserInfoReqVO);
        if (result.getCode().equals(0)) {
            RpcUserInfoVO rpcUserInfoVO = result.getData();
            if (rpcUserInfoVO == null) {
                return new ReturnT(ReturnT.FAIL_CODE, "UserService findUserByEmployeeCode  根据工号：" + employeeCode + "未查到用户信息");
            }
            UserInfo userInfo = new UserInfo();
            userInfo.setUserid(rpcUserInfoVO.getUserId());
            userInfo.setUsername(rpcUserInfoVO.getUserName());
            userInfo.setUserCode(rpcUserInfoVO.getUserAccount());
            if (null != rpcUserInfoVO.getEmployeeInfo()) {
                userInfo.setEmployeeId(rpcUserInfoVO.getEmployeeInfo().getEmployeeId());
            }
            return new ReturnT(userInfo);
        }
        return new ReturnT(ReturnT.FAIL_CODE, "UserService findUserByEmployeeCode  根据工号：" + employeeCode + "未查到用户信息");
    }

    @Override
    public UserInfo getUserInfo(Long userId) {
        SelectUserInfoReqVO selectUserInfoReqVO = new SelectUserInfoReqVO();
        selectUserInfoReqVO.setUserId(userId);
        BaseResult<RpcUserInfoVO> rpcUserInfoVOBaseResult = userFeignService.qryUserInfo(selectUserInfoReqVO);
        RpcUserInfoVO resultData = rpcUserInfoVOBaseResult.getData();
        UserInfo userInfo = null;
        if (!ObjectUtils.isEmpty(resultData)) {
            userInfo = new UserInfo();
            if (resultData.getEmployeeInfo() != null) {
                BeanUtils.copyProperties(resultData.getEmployeeInfo(), userInfo);
            }
            if (!CollectionUtils.isEmpty(resultData.getRoleList())) {
                userInfo.setRoleList(resultData.getRoleList());
            }
            userInfo.setUserid(resultData.getUserId());
            userInfo.setUsername(resultData.getUserName());
            userInfo.setUserAccount(resultData.getUserAccount());
        }
        return userInfo;
    }

    @Override
    public Integer getRegisteredUserCount() {
        BaseResult<Integer> result = userFeignService.getRegisteredUserCount();
        return result.getData();
    }
}
